package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.adapter.FenkeAdapter;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.entity_tab.DBHelps;
import com.xrz.sxm.aj.view.FenKeViewGroup;
import com.xrz.sxm.aj.view.FenKeViewGroup.OnTitlteItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 分科浏览
 *
 * @author 辛汝州 2014-12-18
 */
public class Act_FenKe extends BaseAct implements OnItemClickListener,
		OnTitlteItemClickListener {

	@ViewInject(R.id.fenkeview)
	private FenKeViewGroup m_FenKeViewGroup;
	@ViewInject(R.id.fenke_gridview)
	private GridView m_GridView;

	private FenkeAdapter m_Adapter;

	private String m_IntentDataOld;

	private String m_IntentData;

	private List<String> m_Datas;
	private List<Integer> m_Nums;

	private DBHelps m_DbHelps;
	@Override
	protected int setView() {
		return R.layout.layout_fenke;
	}

	@Override
	protected void init() {
		m_DbHelps= new DBHelps(ctx);
		m_FenKeViewGroup.setOnTitlteItemClickListener(this);
		m_Datas = new ArrayList<String>();
		m_Nums=new ArrayList<Integer>();
		m_IntentData = getIntent().getStringExtra("data");
		m_IntentDataOld = m_IntentData;
		if (m_IntentData.equals("内科症状")) {
			initListDatas();
		}else if(m_IntentData.equals("妇科症状")){
			initFukeListDatas();
		}else{
			m_Datas.addAll(m_DbHelps.getFenleiByName(m_IntentData));
		}
		m_Adapter = new FenkeAdapter(ctx, m_Datas);
		m_GridView.setOnItemClickListener(this);
		m_GridView.setAdapter(m_Adapter);
		m_FenKeViewGroup.addItem(m_IntentData);
	}

	private void initFukeListDatas() {
		m_Datas.add("月经症状");
		m_Datas.add("带下病症状");
		m_Datas.add("妊娠病症状");
		m_Datas.add("产后病症状");
		m_Datas.add("其他症状");
	}

	private void initListDatas() {
		m_Datas.add("头项症状");
		m_Datas.add("四肢症状");
		m_Datas.add("背腰症状");
		m_Datas.add("胸腹症状");
		m_Datas.add("二阴症状");
	}

	@Override
	protected String setTopTitle() {
		return "按分科浏览";
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		String text = m_Datas.get(arg2);
		if(m_IntentData == null){
			m_IntentData = text;
		}
		if(text.equals("内科症状")){
			m_IntentData = "内科症状";
		}else if(text.equals("妇科症状")){
			m_IntentData = "妇科症状";
		}
		if (m_IntentData.equals("内科症状") || m_IntentData.equals("妇科症状")) {
			if (m_FenKeViewGroup.getChildCount() == 5) {
				startActivity(new Intent(this, Act_Detail.class).putExtra("data", text));
				return;
			}
		} else{
			if (m_FenKeViewGroup.getChildCount() == 3) {
				startActivity(new Intent(this, Act_Detail.class).putExtra("data", text));
				return;
			}
		}
		if(text.equals("内科症状")){
			m_FenKeViewGroup.addItem(text);
			m_Datas.clear();
			initListDatas();
			m_Adapter.notifyDataSetChanged();
			return;
		}
		if(text.equals("妇科症状")){
			m_FenKeViewGroup.addItem(text);
			m_Datas.clear();
			initFukeListDatas();
			m_Adapter.notifyDataSetChanged();
			return;
		}
		m_FenKeViewGroup.addItem(text);
		m_Datas.clear();
		m_Datas.addAll(m_DbHelps.getFenleiByName(text));
		m_Adapter.notifyDataSetChanged();
	}

	@Override
	public void onTitleItemClick(String clickText) {
		m_Datas.clear();
		if(clickText.equals("内科症状")){
			initListDatas();
			m_Adapter.notifyDataSetChanged();
			return;
		}
		if(clickText.equals("妇科症状")){
			initFukeListDatas();
			m_Adapter.notifyDataSetChanged();
			return;
		}
		if(clickText.equals("科目")){
			m_Datas.add("内科症状");
			m_Datas.add("妇科症状");
			m_Datas.add("儿科症状");
			m_Datas.add("外科症状");
			m_Datas.add("肛肠科症状");
			m_Datas.add("皮肤科症状");
			m_Datas.add("耳鼻喉科症状");
			m_Datas.add("眼科症状");
			m_Adapter.notifyDataSetChanged();
			m_IntentData = null;
			return;
		}
		m_Datas.addAll(m_DbHelps.getFenleiByName(clickText));
		m_Adapter.notifyDataSetChanged();
	}

}
